layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //用户列表
    var tableIns = table.render({
        elem: '#renwuList',
        url : 'http://127.0.0.1:88/index.php/renwu/renwu/renwuList?id='+localStorage.login_name,
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "renwuListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'renwu_id', title: 'ID' ,width:50, align:"center"},
            {field: 'renwu_good_shop', title: '所属店铺', align:"center"},
            {field: 'renwu_good_searchWord', title: '搜索词', align:'center'},
            {field: 'renwu_good_money', title: '佣金', width:70, align:'center'},
            {field: 'renwu_addTime', title: '添加时间', align:'center'},
            {field: 'renwu_isTake', title: '是否接单', align:'center',templet:function(d){
            	if(d.renwu_isTake==0){
            		return '<button type="button" class="layui-btn layui-btn-sm layui-btn-danger">未接单</button>';
            	}else{
            		return '<button type="button" class="layui-btn layui-btn-xs">已接单</button><button type="button" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="lookrenwu">查看</button>';
            	}
                
            }},
            {field: 'renwu_isDo', title: '是否完成', align:'center',templet:function(d){
            	if(d.renwu_isTake==0){
            		return '<button type="button" class="layui-btn layui-btn-sm layui-btn-danger">未完成</button>';
            	}else{
            		if(d.renwu_isDo==0){
	            		return '<button type="button" class="layui-btn layui-btn-sm layui-btn-danger">未完成</button>';
	            	}else{
	            		if(d.renwu_isCheck==0){
		            		return '<button type="button" class="layui-btn layui-btn-xs">已完成</button><button type="button" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="goCheck">点击审核</button>';
		            	}else{
		            		if(d.renwu_isCheck==1){
		            			return '<button type="button" class="layui-btn layui-btn-radius layui-btn-xs">审核通过</button><button type="button" class="layui-btn layui-btn-radius layui-btn-normal layui-btn-xs" lay-event="lookCheck">查看</button>';
		            		}else{
		            			return '<button type="button" class="layui-btn layui-btn-radius layui-btn-danger layui-btn-xs">审核不通过</button><button type="button" class="layui-btn layui-btn-radius layui-btn-normal layui-btn-xs" lay-event="lookCheck">查看</button>';
		            		}
		            		
		            	}
	            		
	            	}
            	}
            	
                
            }},
            /*{field: 'renwu_isCheck', title: '审核情况', align:'center',templet:function(d){
            	if (d.renwu_isDo==0) {
            		return '<button type="button" class="layui-btn layui-btn-sm layui-btn-danger">前往审核</button>';
            	} else{
            		if(d.renwu_isCheck==0){
	            		return '<button type="button" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="noCheck">前往审核</button>';
	            	}else{
	            		return '<button type="button" class="layui-btn layui-btn-sm">审核完成</button>';
	            	}
            	}
            	
                
            }},*/
            {title: '操作', templet:'#renwuListBar',width:150, align:"center"}
        ]]
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
    	var type = $(this).data('type');
    	//console.log(type);
        if($(".searchVal").val() != ''){
            table.reload("renwuListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                method:'post',
                url:'http://127.0.0.1:88/index.php/renwu/renwu/renwuSearch',
                where: {
                    key: $(".searchVal").val(),  //搜索的关键字
                    user_name:localStorage.login_name
                },
                contentType: 'application/json',
                done: function(res, curr, count){
                  //this.where={};
                  //不清空，有问题：搜索条件会保留
                  //清空，  有问题：分页的时候没有搜索条件了,分页会受到影响
              }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加用户
    function addrenwu(edit){
        var index = layui.layer.open({
            title : "编辑",
            type : 2,
            area:["550px","400px"],
            content : "renwuAdd.html",
            success : function(layero, index){
                /*var body = layui.layer.getChildFrame('body', index);
                body.find(".renwu_name").val(edit.renwu_name);  //登录名
                    body.find(".userEmail").val(edit.userEmail);  //邮箱
                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
                    body.find(".userGrade").val(edit.userGrade);  //会员等级
                    body.find(".userStatus").val(edit.userStatus);    //用户状态
                    body.find(".userDesc").text(edit.userDesc);    //用户简介
                    form.render();*/
                /*if(edit){
                    body.find(".userName").val(edit.userName);  //登录名
                    body.find(".userEmail").val(edit.userEmail);  //邮箱
                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
                    body.find(".userGrade").val(edit.userGrade);  //会员等级
                    body.find(".userStatus").val(edit.userStatus);    //用户状态
                    body.find(".userDesc").text(edit.userDesc);    //用户简介
                    form.render();
                }*/
                setTimeout(function(){
                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        //layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addNews_btn").click(function(){
    	if (localStorage.login_role==1) {
    		layer.msg("无操作权限");
    	} else{
    		addrenwu();
    	}
        
    })
	//刷新renwu_refresh
	$(".renwu_refresh").click(function(){
        tableIns.reload();
    })
	
    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('renwuListTable'),
            data = checkStatus.data,
            renwu_id = [];
            
        if(data.length > 0) {
            for (var i in data) {
                renwu_id.push(data[i].renwu_id);
            }
            layer.confirm('确定删除选中的数据？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("http://127.0.0.1:88/index.php/renwu/renwu/delMoreRenwu",{
                    renwu_id : renwu_id  //将需要删除的renwu_name作为参数传入
                },function(data){
                tableIns.reload();
                layer.close(index);
                })
            })
        }else{
            layer.msg("请选择需要删除的数据");
        }
    })

    //列表操作
    table.on('tool(renwuList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
        	//console.log(data.renwu_id);
        	//console.log(data);
        	if (localStorage.login_role==1) {
        		layer.msg("无操作权限");
        	} else{
        		//console.log(data);
        		if(data.renwu_isTake==1){
        			layer.msg("任务进行中，无法启动编辑");
        		}else if(data.renwu_isDo==1||data.renwu_isCheck==1){
        			layer.msg("任务已完成，无法启动编辑");
        		}else{
        			var index = layui.layer.open({
			            title : "编辑商铺",
			            type : 2,
			            area:["450px","400px"],
			            content : "renwuEdit.html",
			            success : function(layero, index){
			            	//console.log(data);
			                var body = layui.layer.getChildFrame('body', index);
			                body.find(".renwu_good_titles").val(data.renwu_good_titles);
			                body.find(".renwu_good_img").attr("src",data.renwu_img_url);
			                body.find(".renwu_good_price").val(data.renwu_good_price);
			                body.find(".renwu_good_payPrice").val(data.renwu_good_payPrice);
			                body.find(".renwu_good_money").val(data.renwu_good_money);
			                body.find(".renwu_good_searchWord").val(data.renwu_good_searchWord);
			                body.find(".renwu_good_shop").val(data.renwu_good_shop);
			                body.find(".renwu_good_url").val(data.renwu_url);
			                body.find(".renwu_url").val(data.renwu_url);
			                body.find(".renwu_img_url").val(data.renwu_img_url);
			                body.find(".renwu_shop_type").val(data.renwu_shop_type);
			               	body.find(".renwu_yaoqiu").text(data.renwu_yaoqiu);
			               	body.find("#renwu_id").val(data.renwu_id);
			               	form.render();
			                /*if(edit){
			                    body.find(".userName").val(edit.userName);  //登录名
			                    body.find(".userEmail").val(edit.userEmail);  //邮箱
			                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
			                    body.find(".userGrade").val(edit.userGrade);  //会员等级
			                    body.find(".userStatus").val(edit.userStatus);    //用户状态
			                    body.find(".userDesc").text(edit.userDesc);    //用户简介
			                    form.render();
			                }*/
			                setTimeout(function(){
			                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
			                        tips: 3
			                    });
			                },500)
			            }
			        })
        		}
        	}
            
        }else if(layEvent === 'usable'){ //启用禁用
            var _this = $(this),
                usableText = "是否确定禁用此用户？",
                btnText = "已禁用";
            if(_this.text()=="已禁用"){
                usableText = "是否确定启用此用户？",
                btnText = "已启用";
            }
            layer.confirm(usableText,{
                icon: 3,
                title:'系统提示',
                cancel : function(index){
                    layer.close(index);
                }
            },function(index){
                _this.text(btnText);
                layer.close(index);
            },function(index){
                layer.close(index);
            });
        }else if(layEvent === 'del'){ //删除
        	if (data.renwu_isTake==1) {
        		layer.msg("任务进行中，无法启动删除");
        	} else{
        		layer.confirm('确定删除？',{icon:3, title:'提示信息'},function(index){
	                $.get("http://127.0.0.1:88/index.php/renwu/renwu/delRenwu",{
	                    id : data.renwu_id  //将需要删除的newsId作为参数传入
	                },function(data){
	                	if(data.code==200){
	                		layer.msg(data.msg);
	                		setTimeout(function(){
	                			tableIns.reload();
	                    		layer.close(index);
	                		},1000);
	                	}else{
	                		layer.msg(data.msg);
	                	}
	                })
	            });
        	}
        }else if(layEvent === "goCheck"){
        	var index = layui.layer.open({
	            title : "订单审核",
	            type : 2,
	            area:["450px","420px"],
	            content : "renwuCheck.html",
	            success : function(layero, index){
	            	var body = layui.layer.getChildFrame('body', index);
	            	$.get("http://127.0.0.1:88/index.php/renwu/renwu/checkrenwu",{
	                    id : data.renwu_id  //将需要删除的newsId作为参数传入
	                },function(data){
	                	body.find(".dingdanhao").val(data.dingdan[0].dingdanhao);
	                    body.find(".renwu_good_price").val(data.dingdan[0].renwu_good_price);
	                    body.find(".renwu_good_img").attr("src",data.dingdan[0].renwu_img_url);
	                    
	                    body.find(".font_user").val(data.dingdan[0].user_name);
	                    body.find(".font_wangwang").val(data.dingdan[0].user_wangwang);
	                    body.find(".zhifu_jine").val(data.dingdan[0].shifu_jine);
	                    body.find(".dingdan_time").val(data.dingdan[0].timestamp);
	                    body.find(".id").val(data.dingdan[0].renwu_id);
	                    body.find(".user_name").val(localStorage.login_name);
	                    body.find(".dingdan_img").attr("src",data.dingdan[0].dingdan_jietu);
	                    form.render();
	                })
	                
	                setTimeout(function(){
	                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
	                        tips: 3
	                    });
	                },500)
	            }
	        })
        }else if(layEvent === "lookrenwu"){
        	var index = layui.layer.open({
	            title : "接单详情",
	            type : 2,
	            area:["590px","450px"],
	            content : "renwuLook.html",
	            success : function(layero, index){
	            	var body = layui.layer.getChildFrame('body', index);
	            	$.get("http://127.0.0.1:88/index.php/renwu/renwu/lookrenwu",{
	                    id : data.renwu_id  //将需要删除的newsId作为参数传入
	                },function(data){
	                	//console.log(data);
	                	console.log(data.lookRenwu[0].renwu_good_titles);
	                	body.find(".renwu_good_titles").val(data.lookRenwu[0].renwu_good_titles);
		                body.find(".renwu_good_img").attr("src",data.lookRenwu[0].renwu_img_url);
		                body.find(".renwu_good_shop").val(data.lookRenwu[0].renwu_good_shop);
	                    body.find(".user_wangwang").val(data.lookRenwu[0].user_wangwang);
	                    body.find(".take_time").val(data.lookRenwu[0].timestamp);
	                    form.render();
	                })
	                
	                setTimeout(function(){
	                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
	                        tips: 3
	                    });
	                },500)
	            }
	        })
        }else if(layEvent === "lookCheck"){
        	var index = layui.layer.open({
	            title : "订单审核结果",
	            type : 2,
	            area:["590px","450px"],
	            content : "lookCheck.html",
	            success : function(layero, index){
	            	var body = layui.layer.getChildFrame('body', index);
	            	$.get("http://127.0.0.1:88/index.php/renwu/renwu/lookCheck",{
	                    id : data.renwu_id  //将需要删除的newsId作为参数传入
	                },function(data){
	                	
	                	console.log(data);
	                	body.find(".dingdanhao").val(data.dingdan[0].dingdanhao);
	                    body.find(".renwu_good_price").val(data.dingdan[0].renwu_good_price);
	                    body.find(".renwu_good_img").attr("src",data.dingdan[0].renwu_img_url);
	                    
	                    body.find(".font_user").val(data.dingdan[0].user_name);
	                    body.find(".font_wangwang").val(data.dingdan[0].user_wangwang);
	                    body.find(".zhifu_jine").val(data.dingdan[0].shifu_jine);
	                    body.find(".dingdan_time").val(data.dingdan[0].timestamp);
	                    body.find(".id").val(data.dingdan[0].renwu_id);
	                    body.find(".dingdan_img").attr("src",data.dingdan[0].dingdan_jietu);
	                    body.find(".is_pass").val(data.ok);
	                    body.find(".check_reason").text(data.dingdan[0].check_reason);
	                    form.render();
	                })
	                
	                setTimeout(function(){
	                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
	                        tips: 3
	                    });
	                },500)
	            }
	        })
        }
    });

})